﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace SmartSpace
{
    public static class Configuration
    {
        private static string _connectionStringName;
        private static bool? _isAutomaticMigrations;

        /// <summary>
        /// 是否自动更新数据库,默认值是true
        /// <para>
        /// AutomaticMigrations=true
        /// </para>
        /// <para>
        /// AutomaticMigrations=false
        /// </para>
        /// </summary>
        public static bool IsAutomaticMigrations
        {
            get
            {
                if (_isAutomaticMigrations == null)
                {
                    if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AutomaticMigrations"]))
                    {
                        _isAutomaticMigrations = Convert.ToBoolean(ConfigurationManager.AppSettings["AutomaticMigrations"]);
                    }
                    else
                    {
                        _isAutomaticMigrations = true;
                    }
                }
                return _isAutomaticMigrations.Value;
            }
        }

        public static ConnectionStringName ConnectionStringName { get; private set; }

        static Configuration()
        {
            ConnectionStringName = new ConnectionStringName();
        }
    }

    public class ConnectionStringName
    {
        /// <summary>
        /// 默认连接字符串名称:smartspace
        /// </summary>
        public const string DefaultConnectionStringName = "smartspace";

        private string _connectionStringName;

        internal ConnectionStringName()
        {
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ConnectionStringName"]))
            {
                _connectionStringName = ConfigurationManager.AppSettings["ConnectionStringName"];
            }
            else
            {
                _connectionStringName = DefaultConnectionStringName;
            }
        }

        public string this[string key]
        {
            get
            {
                if (ConfigurationManager.ConnectionStrings[key] != null && !string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[key].ConnectionString))
                {
                    return key;
                }
                return _connectionStringName;
            }
        }

        public override string ToString()
        {
            return _connectionStringName;
        }
    }
}
